/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	
	public class Label extends Control
	{
		public function Label()
		{
			tf = new TextField();
			addChild(tf);
			
			NoScale = true;
			
			RequireLayout();
			
			this.doubleClickEnabled = true;
			tf.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, OnDoubleClick);
		}
		
		private var tf:TextField;
		
		public static const MODE_INPUT:int 	  = 0;
		public static const MODE_TEXT:int  	  = 1;
		public static const MODE_EDITABLE:int = 2;
		
		private var mode:int;
		public function get Mode():int { return mode; }
		public function set Mode(value:int):void
		{
			mode = value;
			
			if( mode == MODE_EDITABLE )
			{
				tf.selectable = false;
			}
		}
		
		public function set DefaultTextFormat(value:TextFormat):void { tf.defaultTextFormat = value; }
		
		public function set Multiline(value:Boolean):void { tf.multiline = true; }
		public function set WordWrap(value:Boolean):void  { tf.wordWrap = true; } 
		
		public function get Text():String { return tf.text; }
		public function set Text(value:String):void { tf.text = value; }
		
		public function SetBackground(col:Object):void
		{
			if( col == null ) { tf.background = false; }
			else { tf.background = true; tf.backgroundColor = col as uint; }
		}
		
		protected override function Layout():void
		{
			tf.x = 0;
			tf.y = 0;
			tf.width = width;
			tf.height = height;
		}
		
		private var preEditText:String;
		private function OnDoubleClick(e:MouseEvent):void
		{
			if( mode != MODE_EDITABLE ) return;
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, OnStageMouseDown);
			tf.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
			SetEditMode(true);
		}
		
		private function SetEditMode(on:Boolean):Boolean
		{
			tf.selectable = on;
			tf.type = on? TextFieldType.INPUT : TextFieldType.DYNAMIC;
			
			if( on ) { stage.focus = tf;  tf.setSelection(tf.length, tf.length); }
			else 	 tf.setSelection(0, 0);
			
			if( on ) { preEditText = tf.text; return false; }
			else
			{
				var ret:Boolean = (preEditText != tf.text);
				preEditText = "";
				return ret; 
			}
		}
		
		private function OnStageMouseDown(e:MouseEvent):void
		{
			if( e.target == this || e.target == tf ) return;
			
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, OnStageMouseDown);
			tf.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
			if( SetEditMode(false) ) dispatchEvent(new EnvisageEvent(EnvisageEvent.UPDATED, tf.text, this, null));
		}
		
		private function OnKeyDown(e:KeyboardEvent):void
		{
			if( e.keyCode == Keyboard.ENTER || e.keyCode == Keyboard.TAB ) 
			{
				e.preventDefault();
				
				stage.removeEventListener(MouseEvent.MOUSE_DOWN, OnStageMouseDown);
				tf.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
				if( SetEditMode(false) ) dispatchEvent(new EnvisageEvent(EnvisageEvent.UPDATED, tf.text, this, null));
			}
		}
	}
}








